<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Controller;
use cmf\controller\HomeBaseController;
use app\index\model\ApiModel;
//use app\api\model\ApiModel;
class ApiController extends HomeBaseController
{	
	public function fuli(){
		$request = Request::instance();
      	$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
      	$conf=cmf_get_option("site_info");
      	if(!$user){exit('user_error');}
      	if($request->isPost()){
      		$data=Request::instance()->post();
      		$type=$data['type'];
      		$and_is=Db::name('jinelog')->where(['user'=>$uid,'type'=>$type])->find();
      		if($and_is){
      			$msg=array();
			    $msg['code']='no';
			    $msg['message']='此金额您已经领取过！';
		        echo json_encode($msg);
		        exit;
      		}
      		$sumyj=Db::name('yjjl')->where('user',$uid)->sum('money');
      		$is_ok=0;
      		$money=0;
      		if($type==1){
      			 if($sumyj>100){
      			 	$is_ok=1;
      			 	$money=28.88;
      			 }else{
      			 	$is_ok=0;
      			 }
      		}elseif($type==2){
      			if($sumyj>500){
      			 	$is_ok=1;
      			 	$money=66.66;
      			 }else{
      			 	$is_ok=0;
      			 }
      		}elseif($type==3){
      			if($sumyj>1000){
      			 	$is_ok=1;
      			 	$money=128.88;
      			 }else{
      			 	$is_ok=0;
      			 }
      		}elseif($type==4){
      			if($sumyj>3000){
      			 	$is_ok=1;
      			 	$money=228.88;
      			 }else{
      			 	$is_ok=0;
      			 }
      		}elseif($type==5){
      			if($sumyj>5000){
      			 	$is_ok=1;
      			 	$money=388.88;
      			 }else{
      			 	$is_ok=0;
      			 }
      		}
      		if($is_ok==0){
      			$msg=array();
			    $msg['code']='no';
			    $msg['message']='佣金不足领取此奖励！';
		        echo json_encode($msg);
		        exit;
      		}
      		
  		   $jinearr=array();
  		   $jinearr['user']=$user['id'];
           $jinearr['money']=$money;
           $jinearr['type']=$type;
           $jinearr['time']=date("Y-m-d H:i:s");
           //var_dump($jinearr);die;
          // Db::name('yjjl')->insert($jinearr);
      		Db::name('jinelog')->insert($jinearr);
      	   Db::name('user')->where('id', $user['id'])->setInc('balance',$money);
	    	$msg=array();
		    $msg['code']='ok';
		    $msg['message']='领取成功！';
	        echo json_encode($msg);
	        exit;

      	}
	}
	public function userimg(){
		rand(1111111,9999999);
		$arr=json_decode($result,true);
		if ($arr['code']==1) {
		    echo '<img src="'.$arr['imgurl'].'">';
		} else {
		    echo $arr['msg'];
		}
	}
	public function zhuan(){
		$request = Request::instance();
      	$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
      	$conf=cmf_get_option("site_info");
      	if(!$user){exit('user_error');}
      	if($request->isPost()){
      		$data=Request::instance()->post();
      		$money=$data['txmoney'];
      		$cuid=$data['skuid'];
      		if($user['balance']<2){
      			$msg=array();
			    $msg['code']='no';
			    $msg['message']='转账金额最低2元！';
		        echo json_encode($msg);
		        exit;
      		}
      		if($user['balance']<$money){
      			$msg=array();
			    $msg['code']='no';
			    $msg['message']='金额不足！';
		        echo json_encode($msg);
		        exit;
      		}
      		//查询是否有被转账用户
      		$touser=$userinfo->where('user_login',$cuid)->find();
      		if(!$touser['id']){
      			$msg=array();
			    $msg['code']='no';
			    $msg['message']='用户不存在或输出错误！';
		        echo json_encode($msg);
		        exit;
      		}
      		Db::name('user')->where('id', $user['id'])->setDec('balance',$money);
			Db::name('user')->where('id', $touser['id'])->setInc('balance',$money);
      		$zhuan_log=array();
            $zhuan_log['user']=$user['id'];
            $zhuan_log['cuser']=$touser['id'];
            $zhuan_log['money']=$money;
            $zhuan_log['time']=date('Y-m-d H:i:s');
            Db::name('moneylog')->insert($zhuan_log);
            $user=$userinfo->where('id',$uid)->find();
  			$msg=array();
		    $msg['code']='ok';
		    $msg['message']='成功！';
		    $msg['money']=$user['balance'];
	        echo json_encode($msg);
	        exit;
      	}
		
	}
	
	public function tgimg(){
		
		$request = Request::instance();
      	$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
      	$url=$request->domain();
      	$conf=cmf_get_option("site_info");
      	$urls=$request->domain();
      	// 背景图路径   
        $backPath = ROOT_PATH . 'public/upload/'.$conf['site_admin_tgimg'];
        
        // 获取背景图信息imagecreatefromstring — 从字符串中的图像流新建一图像
        $back = imagecreatefromstring(file_get_contents($backPath));
        list($backWidth, $backHight, $backType) = getimagesize($backPath); //getimagesize — 取得图像大小

        // 引入生成二维码的类库
        include EXTEND_PATH . 'phpqrcode/phpqrcode.php';
        
        // 设置保存二维码文件路径
        $qrcode = ROOT_PATH . 'public/upload/qrcode/'.$uid.'.png';

        $url = $conf['tgurl']."/index/weixin/index.html?qrcode=".$uid."&new=2";
		
         //生成二维码的函数
        \QRcode::png($url, $qrcode,'q',$conf['qrsize'],2);
        
        // 获取二维码的图片信息
        $qrcodeImg = imagecreatefromstring(file_get_contents($qrcode));
        list($qrcodeWidth, $qrcodeHight, $qrcodeType) = getimagesize($qrcode);

        imagecopymerge($back, $qrcodeImg, $conf['qrleft'], $conf['qrtop'], 0, 0, $qrcodeWidth, $qrcodeHight, 100);

        header('Content-Type:image/png');
        imagepng($back,ROOT_PATH.'public/upload/qrcode/qr_'.$uid.'.png');
        // 销毁
        imagedestroy($back);
        imagedestroy($qrcodeImg);
        
        $msg=array();
        $msg['code']="ok";
        $msg['url']="/upload/qrcode/qr_".$uid.'.png';
        echo json_encode($msg);
        exit;
	}
	
	public function jfopen(){
	  $data=Request::instance()->get();
      $uid=session('userid');
      if($data){
        	if($data['openid']){
            	Db::name('user')->where('id', $uid)->update(['txopen' => $data['openid']]);
              	$this->redirect('index/index');
            }
      }
    }
	
	public function ganrao($dat='',$uid)
    {
    	//by luyi QQ:3510978577 仅供学习研究资源分享
    	$conf=cmf_get_option("site_info");
    	$red_array=json_decode($dat['str'],true);
    	$xx=count($red_array)-1;//最后一个数
    	////如果包是机器产生的
    	if ($dat['isrobot']==1) {	
    		if ($dat['user']>0 && $dat['isreal']==0) {
    		  $rand_nom=mt_rand(1,100);
    		  if($rand_nom<=$conf['hbzb']){
    		  		$red_array=dan_lei($dat['money'],$dat['lei']);
    		  }
	          Db::name('newopen')->where('id='.$dat['id'])->setfield('isreal',1);
           }
       }
       if($uid>0) {
          $rand_nom=mt_rand(1,100);
          if ($rand_nom<=$conf['hbzb']) {
             //用户保证中雷
             //把中雷位和最后一个调换--保证中雷
             foreach ($red_array as $kk => $vv) {
              $wei=bcmod($vv*100,10);
              if ($wei==$dat['lei']) {
                 $red_array[$kk]=$red_array[$xx];
                 $red_array[$xx]=$vv;
                 break;
              }else{
                 if ($kk!=$xx) {
                   $red_array[$kk]=$vv;
                 }
              }
            }
          } 
       }
       
       return $red_array;
    		
    }
	public function get_result($dat=0,$uid=0){
		 //系统虚拟产生的订单
		  $red_array=json_decode($dat['str'],true);
		  $xx=count($red_array)-1;//最后一个数
		  //干扰程序开始----------------------------------
		  $red_array=$this->ganrao($dat,$uid);
		  //干扰程序结束----------------------------------
		  $money=0;
		  if ($xx>=0) {
		    $money=$red_array[$xx];
		    unset($red_array[$xx]);
		    $where['id']=$dat['id'];
		    Db::name('newopen')->where($where)->setfield('str',json_encode($red_array));
		  }
		  return $money;
	}
	public function tixian(){
		$request = Request::instance();
      	$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
      	$conf=cmf_get_option("site_info");
      	$txPer = 0.03;//设置提现的百分比
      	
      	if(!$user){exit('user_error');}
      	if($request->isPost()){
      		$data=Request::instance()->post();
      		$txDecMoney = $data['txmoney'] * $txPer;// 提现扣除金额
      		$txmoney = $data['txmoney'] - $txDecMoney;//扣除手续费-》最后提现金额
      		if($user['balance']<2){
      			$msg=array();
			    $msg['code']='no';
			    $msg['message']='提现金额最低2元！';
		        echo json_encode($msg);
		        exit;
      		}
      		if($data['txmoney']<$conf['site_admin_txjine']){
                $msg=array();
			    $msg['code']='no';
			    $msg['message']='提现金额最低'.$conf['site_admin_txjine'];
		        echo json_encode($msg);
		        exit;
            }
            if($data['txmoney']>$user['balance']){
               	$msg=array();
			    $msg['code']='no';
			    $msg['message']='余额不足！';
		        echo json_encode($msg);
		        exit;
            }
               $sumzjin=Db::name('withdraw')->where(['uid'=>$uid,'status'=>1])->whereTime('addtime', 'd')->select();
               $zongtix=count($sumzjin);
               if($zongtix>6){
	               	$msg=array();
				    $msg['code']='no';
				    $msg['message']='当前每日提现次数为5次，请0点后再试，谢谢！';
			        echo json_encode($msg);
			        exit;
                }
               
              // Db::name('user')->where('id', $user['id'])->setDec('balance',$data['txmoney']);
               Db::name('user')->where('id', $user['id'])->setDec('balance',$data['txmoney']);
               $ddh='T'.time().mt_rand(11,99).'_'.$user['id'];
               $arr['order']=$ddh;
               $arr['uid']=$user['id'];
               $arr['addtime']=date("Y-m-d H:i:s");
               $arr['status']=0;
               $arr['money']=$txmoney;
               $arr['type']=1;
               Db::name('withdraw')->insert($arr);
               $witharr=array();
               $witharr['jine']=$txmoney;
               $witharr['txopenid']=$user['txopen'];
               $witharr['user']=$user['id'];
               $with=yunzhifu($witharr);
               //print_r($with);die;
               if($with['code']=="40011"){
               		Db::name('withdraw')->where('order', $ddh)->update(['status' => 1]);
               		$msg=array();
				    $msg['code']='ok';
				    $msg['message']='提现成功';
			        echo json_encode($msg);
			        exit;
               }else if($with['code']=="40010"){
               		Db::name('withdraw')->where('order', $ddh)->update(['status' => 1]);
               		$msg=array();
				    $msg['code']='oks';
				    $msg['message']='等待审核';
			        echo json_encode($msg);
			        exit;
               }else{
               		Db::name('user')->where('id', $user['id'])->setInc('balance',$txmoney);
               		$msg=array();
				    $msg['code']='no';
				    $msg['message']=$with['msg'];
			        echo json_encode($msg);
			        exit;
               }
      	}
      	
      	
	}
	public function yjwiths(){
		$request = Request::instance();
      	$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
      	$conf=cmf_get_option("site_info");
      	if(!$user){exit('user_error');}
      	if($request->isPost()){
      		$data=Request::instance()->post();javascript:;
      		if($user['score']<2){
      			$msg=array();
			    $msg['code']='no';
			    $msg['message']='提现金额最低2元！';
		        echo json_encode($msg);
		        exit;
      		}
      		if($data['txmoney']<$conf['site_admin_txjine']){
                $msg=array();
			    $msg['code']='no';
			    $msg['message']='提现金额最低'.$conf['site_admin_txjine'];
		        echo json_encode($msg);
		        exit;
            }
            if($data['txmoney']>$user['score']){
               	$msg=array();
			    $msg['code']='no';
			    $msg['message']='佣金不足！';
		        echo json_encode($msg);
		        exit;
            }
            
           Db::name('user')->where('id', $user['id'])->setDec('score',$data['txmoney']);
           Db::name('user')->where('id', $user['id'])->setInc('balance',$data['txmoney']);

           $ddh='T'.time().mt_rand(11,99).'_'.$user['id'];
           $arr['order']=$ddh;
           $arr['uid']=$user['id'];
           $arr['addtime']=date("Y-m-d H:i:s");
           $arr['status']=0;
           $arr['money']=$data['txmoney'];
           $arr['type']=2;
           Db::name('withdraw')->insert($arr);
            
        Db::name('withdraw')->where('order', $ddh)->update(['status' => 1]);
   		$msg=array();
	    $msg['code']='ok';
	    $msg['message']='提现成功';
        echo json_encode($msg);
        exit;
        exit();
        die();
               $sumzjin=Db::name('withdraw')->where(['uid'=>$uid,'status'=>1])->whereTime('addtime', 'd')->select();
               $zongtix=count($sumzjin);
               if($zongtix>6){
	               	$msg=array();
				    $msg['code']='no';
				    $msg['message']='当前每日提现次数为5次，请0点后再试，谢谢！';
			        echo json_encode($msg);
			        exit;
                }
               Db::name('user')->where('id', $user['id'])->setDec('score',$data['txmoney']);
               $ddh='T'.time().mt_rand(11,99).'_'.$user['id'];
               $arr['order']=$ddh;
               $arr['uid']=$user['id'];
               $arr['addtime']=date("Y-m-d H:i:s");
               $arr['status']=0;
               $arr['money']=$data['txmoney'];
               $arr['type']=2;
               Db::name('withdraw')->insert($arr);
               $witharr=array();
               $witharr['jine']=$data['txmoney'];
               $witharr['txopenid']=$user['txopen'];
               $witharr['user']=$user['id'];
               $with=yunzhifu($witharr);
               //print_r($with);
               if($with['code']=="40011"){
               		Db::name('withdraw')->where('order', $ddh)->update(['status' => 1]);
               		$msg=array();
				    $msg['code']='ok';
				    $msg['message']='提现成功';
			        echo json_encode($msg);
			        exit;
               }else{
               		Db::name('user')->where('id', $user['id'])->setInc('score',$data['txmoney']);
               		$msg=array();
				    $msg['code']='no';
				    $msg['message']=$with['msg'];
			        echo json_encode($msg);
			        exit;
               }
      	}
      	
      	
	}
	public function chongzhi(){
		$request = Request::instance();
		$data=Request::instance()->post();
		$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
      	$type=$data['cztype'];
      	$money=$data['czmoney'];
      	$ddh=time()."_".$money."_".$uid;
		//加载fastpay支付插件
		if (!function_exists('get_openid')) {
		require $_SERVER['DOCUMENT_ROOT'].'/fastpay/Fast_Cofig.php';
		}
		
		$paydata=array();
		$paydata['uid']=$uid;//支付用户id
		$paydata['order_no']=$ddh;//订单号
		$paydata['total_fee']=$money;//金额
		$paydata['param']="支付";//其他参数
		$paydata['me_back_url']="http://".$_SERVER['HTTP_HOST'];//支付成功后跳转
		$paydata['notify_url']="http://".$_SERVER['HTTP_HOST']."/index/pay/notify";//支付成功后异步回调
		
		$geturl=fastpay_order($paydata);//获取支付链接
		
        $arr['order']=$ddh;
      	$arr['uid']=$uid;
      	$arr['addtime']=date("Y-m-d H:i:s");
      	$arr['status']=0;
      	$arr['money']=$money;
      	$arr['price']=$money;
      	$arr['type']=$type;
      	Db::name('pay')->insert($arr);
		
		
		$msg=array();
  		$msg['code']='ok';
	    $msg['type']='dsf';
	    $msg['payimg']=$geturl;
	    $msg['money']=$money;
        echo json_encode($msg);
        exit;
		//exit("<meta http-equiv='Refresh' content='0;URL={$geturl}'>");
	}
	public function chamx(){
		$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
      	$buy_game_arr=array();
      	$paylogs=Db::name('neworders')->where('user',$user['id'])->order('id desc')->select()->toArray();
      	foreach ($paylogs as $value){
      			$hbsarr=array();
	      		if($value['zl']==1){
	      			$new_islei=Db::name('newopen')->where('id',$value['lid'])->find();
	      			$newmoneys=$new_islei['money']*1.6-$value['money'];
	      			$hbsarr['money']='收益-'.$newmoneys;
	      			$hbsarr['beizhu']="抢包中雷";
	      		}else{
	      			$hbsarr['beizhu']="抢包未中";
	      			$hbsarr['money']='收益+'.$value['money'];
	      		}
		      	$hbsarr['mxtime']=$value['addtime'];
		      	array_push($buy_game_arr,$hbsarr);
      	}
    	$paylogs=Db::name('newopen')->where('user',$user['id'])->order('id desc')->select()->toArray();
      	foreach ($paylogs as $value){
      			$hbsarr=array();
      			if($value['moneys']>1){
      				$hbsarr['money']="收益 +".$value['moneys'];
      			}else{
      				$hbsarr['money']="收益 -".$value['money'];
      			}
	      		$hbsarr['beizhu']="发包";
		      	$hbsarr['mxtime']=date('Y-m-d H:i:s',$value['addtime']);
		      	array_push($buy_game_arr,$hbsarr);
      	}
      	$ctime_str = array();
		foreach($buy_game_arr as $key=>$v){
		    $buy_game_arr[$key]['ctime_str'] = strtotime($v['mxtime']);
		    $ctime_str[] = $buy_game_arr[$key]['ctime_str'];
		}
		array_multisort($ctime_str,SORT_DESC,$buy_game_arr);
		$yj_web_log=array();
		$yj_logs=Db::name('yjjl')->where('user',$user['id'])->order('id desc')->select()->toArray();
		foreach ($yj_logs as $value){
      			$hbsarr=array();
      			$hbsarr['type']=4;
	      		$hbsarr['money']=$value['money'];
	      		$hbsarr['beizhu']=$value['type'].",".$value['account'];
		      	$hbsarr['mxtime']=$value['addtime'];
		      	array_push($yj_web_log,$hbsarr);
      	}
		//$paylogs
		$cz_web_log=array();
		$cz_sql=Db::name('pay')->where(['uid'=>$user['id'],'status'=>1])->order('id desc')->select()->toArray();
		foreach ($cz_sql as $value){
				$hbsarr=array();
      			$hbsarr['order']=$value['order'];
	      		$hbsarr['money']=$value['money'];
	      		$hbsarr['beizhu']="充值".$value['money']."金币";
		      	$hbsarr['mxtime']=$value['addtime'];
		      	array_push($cz_web_log,$hbsarr);
		}
		$zz_sql=Db::name('moneylog')->where(['user'=>$user['id']])->order('id desc')->select()->toArray();
		foreach ($zz_sql as $value){
				$hbsarr=array();
				$zz_user=Db::name('user')->where('id',$value['cuser'])->find();
      			$hbsarr['order']='对方名称:'.$zz_user['user_nickname'];
	      		$hbsarr['money']=$value['money'];
	      		$hbsarr['beizhu']="转账给".$zz_user['user_login']."-".$value['money']."金币";
		      	$hbsarr['mxtime']=$value['time'];
		      	$hbsarr['type']=5;
		      	array_push($cz_web_log,$hbsarr);
		}
		$with_web_log=array();
		$with_sql=Db::name('withdraw')->where(['uid'=>$user['id'],'status'=>1])->order('id desc')->select()->toArray();
		foreach ($with_sql as $value){
				$hbsarr=array();
      			$hbsarr['order']=$value['order'];
	      		$hbsarr['money']=$value['money'];
	      		$hbsarr['beizhu']="提现".$value['money']."金币";
		      	$hbsarr['mxtime']=$value['addtime'];
		      	array_push($with_web_log,$hbsarr);
		}
      	$msg=array();
  		$msg['code']='ok';
	    $msg['mx_a']=$buy_game_arr;
	    $msg['mx_b']=$cz_web_log;
	    $msg['mx_c']=$with_web_log;
	    $msg['mx_d']=$yj_web_log;
        echo json_encode($msg);
        exit;
	}
	public function kai(){
		$request = Request::instance();
		$data=Request::instance()->post();
		$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
      	$hb_id=$data['hbid'];
      	$hongbao=Db::name('newopen')->where('id',$hb_id)->find();
      	if($hongbao['num']>=7){
      		$msg=array();
		    $msg['code']='no';
		    $msg['message']='红包已被抢完！';
	        echo json_encode($msg);
	        exit;
      	}
      	if($hongbao['user']==$uid){
      		$msg=array();
		    $msg['code']='no';
		    $msg['message']='自己发的红包不能抢哟！';
	        echo json_encode($msg);
	        exit;
      	}
      	//判断金额是否足够
      	$buy_money=$hongbao['money']*1.6;
      	if($user['balance']<$buy_money){
			$msg=array();
		    $msg['code']='no';
		    $msg['message']='金币不足'.$buy_money."！";
	        echo json_encode($msg);
	        exit;
		}
		$myqiang=Db::name('neworders')->where(['lid'=>$hb_id,'user'=>$uid])->find();
		if($myqiang){
			$msg=array();
		    $msg['code']='no';
		    $msg['message']="已经领取过了";
	        echo json_encode($msg);
	        exit;
		}
		
		$kai_bao_arr=json_decode($hongbao['str']);
		//进入作弊干扰
		$ok_moeny=$this->get_result($hongbao,$uid);
		$where['id']=$hongbao['id'];
		Db::name('newopen')->where($where)->setInc('num');
		$hongbao=Db::name('newopen')->where('id',$hb_id)->find();
      	if($hongbao['num']>=7){
      		$msg=array();
		    $msg['code']='no';
		    $msg['message']='红包已被抢完！';
	        echo json_encode($msg);
	        exit;
      	}
		if($ok_moeny>0){
			//中雷
			$buy_wei=bcmod($ok_moeny*100,10);
			if($hongbao['lei']==$buy_wei){
				$zhoglei=1;
				$peikuan=$hongbao['money']*1.5;
				$userjine=Db::name('user')->where('id', $uid)->setDec('balance',$peikuan);
				//中雷了给发包用户加钱并增加记录
				$kai_user_zlei=array();
				$kai_user_zlei['user']=$hongbao['user'];
				$kai_user_zlei['hbid']=$hongbao['id'];
				$kai_user_zlei['cuser']=$uid;
				$kai_user_zlei['time']=time();
				$kai_user_zlei['money']=$peikuan;
				$sql=Db::name('newloding')->insert($kai_user_zlei);
				$userjine=Db::name('user')->where('id', $hongbao['user'])->setInc('balance',$peikuan);
				Db::name('newopen')->where('id', $hongbao['id'])->setInc('moneys',$peikuan);

			}else{
				$peikuan=0;
				$zhoglei=0;
			}
			
			$userjine=Db::name('user')->where('id', $uid)->setInc('balance',$ok_moeny);
			$conf=cmf_get_option("site_info");
			$u=Db::name('user')->where(['id'=>$uid])->find();
              for($i=0;$i<6;$i++){
               $price=$ok_moeny*$conf['site_admin_yj'.$i]/100;
               $u1=Db::name('user')->where(['id'=>$u['ss'.$i]])->find();
                  if($u1){
                    Db::name('user')->where(['id'=>$u['ss'.$i]])->setInc("score",$price);
                    $datas['user']=$u['ss'.$i] ;
                    $datas['money']=$price;
                    $datas['type']=$i+1;
                    $datas['addtime']=date("Y-m-d H:i:s");
                    $datas['account']=($i+1)."级返佣(抢包)";
                    Db::name('yjjl')->insert($datas);
                  }
              }
		}
		$sql_kailog=array();
		$sql_kailog['user']=$user['id'];
      	$sql_kailog['account']=$peikuan;
      	$sql_kailog['lid']=$hongbao['id'];
      	$sql_kailog['state']=1;
      	$sql_kailog['money']=$ok_moeny;
      	$sql_kailog['addtime']=date('Y-m-d H:i:s');
      	$sql_kailog['orders']="L".date('YmdHis').rand(1111,9999);
      	$sql_kailog['ip']=$request->ip();
      	$sql_kailog['times']=time();
      	$sql_kailog['zl']=$zhoglei;
      	$sql_kailog['term']=$hongbao['id'];
      	$sql=Db::name('neworders')->insert($sql_kailog);
      	
		$user=$userinfo->where('id',$uid)->find();
  		$msg=array();
	    $msg['code']='ok';
	    $msg['UserMoney']=$user['balance'];
	    $msg['money']=$ok_moeny;
	    $msg['lqtime']=date('Y-m-d H:i:s');
	    $msg['zlei']=$zhoglei;
	    $msg['peikuan']=$peikuan;
        echo json_encode($msg);
        exit;
      	
	} 
	
	public function fa(){
		$data=Request::instance()->post();
		$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
		$buymoney=$data['fa_money'];
		$buylei=$data['fa_leinum'];
		if($user['balance']<$buymoney){
			$msg=array();
		    $msg['code']='no';
		    $msg['message']='金额不足！';
	        echo json_encode($msg);
	        exit;
		}
		$numdh=time().rand(111,999);
		$conf=cmf_get_option("site_info");
		
		$red_list=hongbao($buymoney,7);
		$rand_nom=mt_rand(1,100);
        if($rand_nom<=$conf['hbzb']) {
          	$red_list=pai_lei($buymoney,$buylei);
        }
 		$buy_arr=array();
    	$buy_arr['lei']=$buylei;
      	$buy_arr['money']=$buymoney;
        $buy_arr['moneys']=0;
        $buy_arr['addtime']=time();
      	$buy_arr['user']=$user['id']; 
      	$buy_arr['num']=0;
      	$buy_arr['str']=json_encode($red_list);
      	$buy_arr['note']=json_encode($red_list);
      	$buy_arr['isrobot']=2;
      	$buy_arr['ornum']=$numdh;
      	$buy_arr['type']=2;
       	$sql=Db::name('newopen')->insert($buy_arr);
		$fa_hb=Db::name('newopen')->where('ornum',$numdh)->find();
		if($fa_hb){
			$userjine=Db::name('user')->where('id', $uid)->setDec('balance',$data['fa_money']);
			$user=$userinfo->where('id',$uid)->find();
			//返佣部分
			
			$u=Db::name('user')->where(['id'=>$uid])->find();
              for($i=0;$i<6;$i++){
               $price=$buymoney*$conf['site_admin_yj'.$i]/100;
               $u1=Db::name('user')->where(['id'=>$u['ss'.$i]])->find();
                  if($u1){
                    Db::name('user')->where(['id'=>$u['ss'.$i]])->setInc("score",$price);
                    $datas['user']=$u['ss'.$i] ;
                    $datas['money']=$price;
                    $datas['type']=$i+1;
                    $datas['addtime']=date("Y-m-d H:i:s");
                    $datas['account']=($i+1)."级返佣(发包)";
                    Db::name('yjjl')->insert($datas);
                  }
              }
			
			$msg=array();
		    $msg['code']='ok';
		    $msg['hbid']=$fa_hb['id'];
		    $msg['UserMoney']=$user['balance'];
	        echo json_encode($msg);
	        exit;
		}
		
		
	}
	public function king(){

			for($i=2;$i<=1000;$i++){
					$login_name=sha1(uniqid());
					$login_name2=substr($login_name,1,3);
					$login_name1=substr($login_name,-3,-1);
					$user_login=$login_name1.$login_name2;
					if(rand(1,3)==2){
						$user_login=strtolower($user_login);
					}else{
						$user_login=strtoupper($user_login);
					}
				Db::name('user')->where('id', $i)->update(['user_login' => $user_login]);
			}
		
	}
	public function info(){
		$data=Request::instance()->get();
		$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
      	$conf=cmf_get_option("site_info");
      	
      	$sumyj=Db::name('yjjl')->where('user',$uid)->sum('money');
      	$sumzjin=Db::name('orders')->where('user',$uid)->whereTime('addtime', 'd')->sum('zj');
      	
      	$ret[0]=$userinfo->where(['ss0'=>$uid])->count('id');
		$ret[1]=$userinfo->where(['ss1'=>$uid])->count('id');
		$ret[2]=$userinfo->where(['ss2'=>$uid])->count('id');
		$ret[3]=$userinfo->where(['ss3'=>$uid])->count('id');
		$ret[4]=$userinfo->where(['ss4'=>$uid])->count('id');
		$ret[5]=$userinfo->where(['ss5'=>$uid])->count('id');
		$ret[6]=$userinfo->where(['ss6'=>$uid])->count('id');
      	
      	$yongjin_1=Db::name('yjjl')->where(['user'=>$uid,'type'=>1])->sum('money');
      	$yongjin_2=Db::name('yjjl')->where(['user'=>$uid,'type'=>2])->sum('money');
      	$yongjin_3=Db::name('yjjl')->where(['user'=>$uid,'type'=>3])->sum('money');
      	$yongjin_4=Db::name('yjjl')->where(['user'=>$uid,'type'=>4])->sum('money');
      	$yongjin_5=Db::name('yjjl')->where(['user'=>$uid,'type'=>5])->sum('money');
      	$yongjin_6=Db::name('yjjl')->where(['user'=>$uid,'type'=>6])->sum('money');
      	$yongjin_7=Db::name('yjjl')->where(['user'=>$uid,'type'=>7])->sum('money');
      	
      	$userarr=array();
      	$userarr['money']=$user['balance'];
      	$userarr['yjsore']=$user['score'];
      	$userarr['ban']='2';
      	$userarr['uid']=$user['user_nickname'];
      	$userarr['fuli']="0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
      	$userarr['shangji']=$user['ss0'].",".$user['ss1'];
      	$userarr['yjmoney']=$yongjin_1.",".$yongjin_2.",".$yongjin_3.",".$yongjin_4.",".$yongjin_5.",".$yongjin_6.",".$yongjin_7;
      	$userarr['xiaji']=$ret[0].",".$ret[1].",".$ret[2].",".$ret[3].",".$ret[4].",".$ret[5].",".$ret[6];
      	$userarr['timemoney']=date('Y-m-d H:i:s');
		$userarr['daymoney']=$sumzjin;
		
		
      	$conarr=array();
      	$conarr['sygg']=$conf['site_icp'];
      	$conarr['cztype']="wx";
		$conarr['yjbl']=$conf['site_admin_yj0'].",".$conf['site_admin_yj1'].",".$conf['site_admin_yj2'].",".$conf['site_admin_yj3'].",".$conf['site_admin_yj4'].",".$conf['site_admin_yj5'].",".$conf['site_admin_yj6'];
		$conarr['kfimg']="upload/".$conf['ddimg'];
		$conarr['kfimg2']="upload/".$conf['site_admin_kfimg'];
      	$conarr['wfimg']="upload/".$conf['wanfaimg'];
      	$conarr['czimg']="upload/".$conf['czkfimg'];
      	
	    $msg=array();
	    $msg['code']='ok';
	    $msg['UserInfo']=$userarr;
	    $msg['config']=$conarr;
        echo json_encode($msg);
        exit;
		
		exit();
	}
	public function fabao(){
		//exit();
		$data=Request::instance()->get();
		$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();

      	$hongbaoarr=array();
      	$hongbao_buy=array();
      	$openlist=Db::name('newopen')->order('id desc')->limit(16)->select()->toArray();
      	foreach ($openlist as $value){
      		$msarr=array();
      		$hongnbao_user=$userinfo->where('id',$value['user'])->find();
      		$msarr['Id']=$value['id'];
      		$msarr['uid']=$hongnbao_user['user_nickname'];
      		$msarr['userimg']=$hongnbao_user['avatar'];
      		$msarr['type']=$value['type'];
      		$msarr['money']=$value['money'];
      		$msarr['moneys']=$value['moneys'];
      		$msarr['lei']=$value['lei'];
      		$msarr['number']=$value['num'];
      		$msarr['fatime']=date('Y-m-d H:i:s',$value['addtime']);
      		
      		$paylogs=Db::name('neworders')->where('lid',$value['id'])->order('id desc')->select()->toArray();
      		foreach ($paylogs as $value){
	      		$hbsarr=array();
	      		$hongnbao_user=$userinfo->where('id',$value['user'])->find();
	      		$hbsarr['uid']=$hongnbao_user['user_nickname'];
	      		$hbsarr['userimg']=$hongnbao_user['avatar'];
	      		$hbsarr['money']=$value['money'];
	      		$hbsarr['hbid']=$value['lid'];
	      		$hbsarr['lqtime']=$value['addtime'];
	      		$hbsarr['Id']=$value['id'];
		      	$hbsarr['zlei']=$value['zl'];
		      	
		      	array_push($hongbao_buy,$hbsarr);
	      	}
      		
      		array_push($hongbaoarr,$msarr);
      	}
      	
      	
		$hongbaoarr = array_reverse($hongbaoarr); 
		$hongbao_buy = array_reverse($hongbao_buy); 
      	
	    $msg=array();
	    $msg['code']='ok';
	    $msg['balance']=$user['balance'];
	    $msg['honbao']=$hongbaoarr;
	    $msg['honbaos']=$hongbao_buy;
        echo json_encode($msg,true);
        exit;
		
		exit();
	}
	
	public function notify(){
	   $data=Request::instance()->get();
	   $arr['key']=$data['key'];
	   $arr['fujian']=$data['record'];
	   $arr['money']=$data['money'];
	   $arr['money_index']=$data['money_index'];
	   $co=(new Config)->where(['id'=>1])->find();
	   echo $co['kkey'];
	   if($data['key']==$co['kkey']){
		   (new Pay)-> paynot($arr);
	   }else{
		   
		   echo "keyerror";
	   }
   }

	public function newtest(){ 
    	if(empty($_GET['pk'])){
    		exit('错误');
    	}
      	if($_GET['pk']==21009){
        	Db::name('user')->where('id','>',1000)->delete();	
          	echo '数据清理完成ok';
        }
      	//结束
	}
	
	public function set_lei($ff='',$lei_wei){
	$iflei=0;
	  if (count($ff)) {
	       foreach ($ff as $key => $value) {
	            $wei=bcmod($value*100,10);
	            if ($wei==$lei_wei) {
	               $iflei=1;
	            }
	         $big_ff[$key]=100*$value;//扩大100倍
	       }
	       //随机金额不存在雷的情况下
	       if ($iflei==0) {
	              $now_wei=bcmod($big_ff[0],10);
	              if ($now_wei>$lei_wei) {
	                 $cha=$now_wei-$lei_wei;
	                 $big_ff[0]=$big_ff[0]-$cha;
	                 $dec_inc=1;
	              } else {
	                 $cha=$lei_wei-$now_wei;
	                 $big_ff[0]=$big_ff[0]+$cha;
	                 $dec_inc=0;
	              }
	              foreach ($big_ff as $kx => $vx) {
	                 if ($kx>0) {
	                    $sec=bcmod($vx,10);
	                    if ($dec_inc==1) {
	                       if(($sec+$cha)<=9){
	                        $big_ff[$kx]=$vx+$cha;
	                        break;
	                       }
	                    } else {
	                       if(($sec-$cha)>=1){
	                        $big_ff[$kx]=$vx-$cha;
	                        break;
	                       }
	                    }
	                 }        
	              }
	
	       }
	 //缩小100倍
	   foreach ($big_ff as $bk => $bv) {
	     $ff[$bk]=''.sprintf("%.2f",round(($bv/100),2));//扩大100倍
	   }
	   shuffle($ff);
	  }
	 
	  return $ff;
  }
	
  	
	
	
}
